package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo22 {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10000);
        //生产者生产元素
        Thread producer = new Thread(()->{
            int n = 0;
            while(true){
                try {
                    queue.put(n);
                    System.out.println("生产元素" + n);
                    //Thread.sleep(1000);
                    n++;
                } catch (InterruptedException e) {
                    throw new RuntimeException();
                }
            }
        },"producer");

        //消费者消费元素
        Thread consumer = new Thread(()->{
            while(true){
                try {
                    Integer n = queue.take();
                    System.out.println("消费元素" + n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException();
                }
            }
        },"consumer");

        producer.start();
        consumer.start();
    }
}
